package top.goldenyear.porpoise.framework.security.core.util;

import com.alibaba.ttl.TransmittableThreadLocal;
import top.goldenyear.porpoise.framework.common.model.LoginUser;

/**
 * @author airhead
 */
public class UserContext {
  /** 当前租户编号 */
  private static final ThreadLocal<LoginUser> LOGIN_USER = new TransmittableThreadLocal<>();

  public static LoginUser getLoginUser() {
    return LOGIN_USER.get();
  }

  public static void setLoginUser(LoginUser loginUser) {
    LOGIN_USER.set(loginUser);
  }

  public static Integer getLoginUserId() {
    if (getLoginUser() != null) {
      return getLoginUser().getId();
    }

    return null;
  }

  public static String getLoginUserName() {
    if (getLoginUser() != null) {
      return getLoginUser().getUserName();
    }

    return "";
  }

  public static String getLoginNickName() {
    if (getLoginUser() != null) {
      return getLoginUser().getNickName();
    }

    return "";
  }

  public static void clear() {
    LOGIN_USER.remove();
  }
}
